package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "购物车")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result<String> addCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("[添加购物车] shoppingCartDTO:{}", shoppingCartDTO);
        Integer tage = shoppingCartService.addCart(shoppingCartDTO);
        return Result.success(tage > 0 ? "添加成功" : "添加失败");
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> findCart() {
        log.info("[查看购物车]");
        List<ShoppingCart> all = shoppingCartService.findAll();
        return Result.success(all);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean() {
        log.info("[清空购物车]");
        Integer tage = shoppingCartService.clean();
        return tage > 0 ? Result.success("清空购物车成功") : Result.error("清空购物车失败");
    }



}
